package com.qingjiang.projectschedule.controller;

import com.qingjiang.projectschedule.pojo.*;
import com.qingjiang.projectschedule.service.*;
import com.qingjiang.projectschedule.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("project")
@CrossOrigin
public class ProjectController {
    @Autowired
    private ProjectInformationService informationService;

    @Autowired
    private ProjectScheduleService scheduleService;

    @Autowired
    private ProjectNumService numService;

    @Autowired
    private NodeDelayService nodeDelayService;

    @Autowired
    private ProjectNodeService projectNodeService;

    @PostMapping("addOne")
    public Result addOne(@RequestBody ProjectAllInformation projectAllInformation) {
        if (informationService.checkProjectIsAdded(projectAllInformation.getProjectName())) {
            projectAllInformation.setProjectId(numService.generateProjectId());
            if (informationService.addOne(projectAllInformation) && scheduleService.addOne(projectAllInformation))
                return Result.ok(null);
        }
        return Result.build(null, 444, "项目已导入！", false);
    }

    @PostMapping("updateProject")
    public Result updateProject(@RequestBody ProjectAllInformation projectAllInformation) {
//        projectAllInformation.setProjectId(numService.generateProjectId());
        System.out.println("更新项目：" + projectAllInformation);
        if (informationService.updateProject(projectAllInformation) && scheduleService.updateProject(projectAllInformation))
            return Result.ok(null);
        return Result.error(null);
    }

    @PostMapping("reportProject")
    public Result reportProject(@RequestBody ProjectAllInformation projectAllInformation) {
        System.out.println("项目汇报" + projectAllInformation);
        if (scheduleService.reportProject(projectAllInformation)) {
            return Result.ok(null);
        }
        return Result.error(null);
    }

    @GetMapping("getExcelTemplate")
    public void getExcelTemplate(HttpServletResponse response) throws IOException {
        informationService.getExcelTemplate(response);
    }

    @PostMapping("uploadExcel")
    public Result uploadExcel(@RequestParam("file") MultipartFile file) {
        return informationService.uploadExcel(file);
    }

    @GetMapping("list/update/isCheck")
    public Result updateIsCheck(ProjectInformation projectInformation) {
        System.out.println(projectInformation.toString());
        if (informationService.setIscheck(projectInformation.getProjectId(), 1 - projectInformation.getIsCheck())) {
            return Result.ok(null);
        }
        return Result.error(null);
    }

    @GetMapping("list/update/isCancel")
    public Result updateIsCancel(ProjectInformation projectInformation) {
        System.out.println(projectInformation.toString());
        if (informationService.setIscancel(projectInformation.getProjectId(), 1 - projectInformation.getIsCancel())) {
            return Result.ok(null);
        }
        return Result.error(null);
    }

    @PostMapping("list/update/nodeFinish")
    public Result updateNodeFinish(@RequestBody NodeOperation nodeOperation) {
        //fixme:节点实际完成时间不得早于节点开始时间
        System.out.println("更新节点实际开始结束时间");
        System.out.println(nodeOperation.toString());
        String res = scheduleService.nodeFinish(nodeOperation);
        if (res.equals("ok") && nodeOperation.getEndTime() != null && !nodeOperation.getEndTime().isEmpty()) {
            if (informationService.setCurrentNodeid(nodeOperation.getProjectId(), nodeOperation.getNodeId())) {
                return Result.ok(null);
            }
        } else {
            return Result.ok(null);
        }
        return Result.build(null, 201, res, false);
    }


    @GetMapping("list")
    public Result queryProject(@RequestParam Map<String, Object> params) {
        System.out.println(params.toString());
        String project_id = (String) params.get("project_id");
        String OP = (String) params.get("OP");
        if (OP.equals("getAll")) {
            List<ShowProjectAllInformation> project_unfinish = informationService.query_unfinished();
            return Result.ok(project_unfinish);
        }
        return Result.error(null);
    }

    @PostMapping("query/byCond")
    public Result queryProjectByCond(@RequestBody ProjectQueryCond projectQueryCond) {
        System.out.println("条件查询:" + projectQueryCond.toString());
        Map<String, Object> res = informationService.queryByCond(projectQueryCond);
        return Result.ok(res);
    }

    @PostMapping("list/update/delayReason")
    public Result updateDelayReason(@RequestBody NodeOperation nodeOperation) {
        System.out.println(nodeOperation.toString());
        if (scheduleService.setDelayReason(nodeOperation)) {
            return Result.ok(null);
        }
        return Result.error(null);
    }

    @PostMapping("update/cancelReason")
    public Result updateCancelReason(@RequestBody NodeOperation nodeOperation) {
        System.out.println("项目取消：" + nodeOperation.toString());
        if (informationService.setCancelReason(nodeOperation)) {
            return Result.ok(null);
        }
        return Result.error(null);
    }

    @PostMapping("list/update/postponeApply")
    public Result postponeApply(@RequestBody NodeOperation nodeOperation) {
        System.out.println(nodeOperation.toString());
        if (nodeDelayService.postponeApply(nodeOperation)) {
            return Result.ok(null);
        }
        return Result.error(null);
    }

    @GetMapping("projectNode/getNodeName")
    public Result getNodeName(@RequestParam("nodeId") int nodeId) {
        System.out.println(nodeId);
        String nodeName = projectNodeService.getNodeNameById(nodeId);
        return Result.ok(nodeName);
    }

    @GetMapping("getLockedProject")
    public Result getLockedProject(@Param("page") Integer page, @Param("pageSize") Integer pageSize) {
        return informationService.getLockedProject(page, pageSize);
    }

    @GetMapping("unLockProject")
    public Result unLockProject(@Param("projectId") String projectId) {
        return informationService.unLockProject(projectId);
    }
}
